package com.example.tys.baseapplication;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.tys.baseapplication.Mvp.Presenter.TestPresent;
import com.example.tys.baseapplication.Mvp.View.interfaces.ITestView;
import com.example.tys.baseapplication.NetModel.executor.CallBack;
import com.example.tys.baseapplication.NetModel.executor.rxandroid.RxAndroidExe;
import com.example.tys.baseapplication.NetModel.network.ClientException;
import com.example.tys.baseapplication.NetModel.network.INetOperation;
import com.example.tys.baseapplication.NetModel.network.NetServer;
import com.example.tys.baseapplication.View.banner.BannerView;
import com.example.tys.baseapplication.View.recycleView.sticky.StickyAdapter;
import com.example.tys.baseapplication.View.recycleView.sticky.StickyItemDivider;
import com.example.tys.baseapplication.View.recycleView.swipe.SwipeRecyclerView;
import com.example.tys.baseapplication.View.recycleView.swipe.TestSwipeAdapter;
import com.example.tys.baseapplication.bean.Test;
import com.example.tys.baseapplication.callback.PermissionCallBack;
import com.example.tys.baseapplication.fragment.BaseFragment;
import com.tbruyelle.rxpermissions2.Permission;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.operators.observable.ObserverResourceWrapper;
import io.reactivex.observers.ResourceObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Response;


public class MainActivity extends BaseActivity<TestPresent> implements ITestView {
    private final CompositeDisposable disposables = new CompositeDisposable();
    private Integer ret = 0;
    private BannerView banner;
    private SwipeRecyclerView mRecyclerView;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate");

        Log.e("----", Thread.currentThread().getName() + "_ " +
                Thread.currentThread().getId()
        );

        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String body = intent.getStringExtra("body");
        Log.e(TAG, title + " _ " + body);

        banner = (BannerView) findViewById(R.id.banner);

        int res[] = {
                R.drawable.c1, R.drawable.c2,
                R.drawable.c3, R.drawable.c4
        };
        banner.addBannerImage(res);
        banner.setBannerViewClickListen(new BannerView.BannerViewClickListen() {
            @Override
            public void onBannerViewClick(int index) {
                Toast.makeText(MainActivity.this, "" + index, Toast.LENGTH_SHORT).show();
            }
        });


        mRecyclerView = (SwipeRecyclerView) findViewById(R.id.recycler);
        final ArrayList<String> data = new ArrayList<>();
        StickyAdapter adapterData = new TestSwipeAdapter(this, data);
        Random ran = new Random();
        for (int i = 0; i < 5; i++) {
            data.add("" + i);
        }
        Collections.sort(data);
        mRecyclerView.addItemDecoration(new StickyItemDivider(adapterData));

//        mRecyclerView.addItemDecoration(new DividerGridItem(this));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(adapterData);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String title = intent.getStringExtra("title");
        String body = intent.getStringExtra("body");
        Log.e(TAG, "onNewIntent" + " " + title + " " + body);
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "onDestroy");

        banner.cancelScan();
        super.onDestroy();
    }

    @Override
    TestPresent createPresent() {
        return new TestPresent(this);
    }

    public void onclick(View view) {

        startActivity(new Intent(this, Main2Activity.class));

    }

    public void callback(View view) {
//        presenter.loadData();

        new RxAndroidExe<Test>(new CallBack<Test>() {
            @Override
            public void onSuccess(Test result) {
                Log.e("0-0-0-", result.toString());
            }

            @Override
            public void onFailed(Throwable e) {
                Log.e("0-0-0-", e.toString());
                Toast.makeText(MainActivity.this, e.getMessage(), 1).show();
            }
        }) {
            @Override
            protected Response doTask() throws ClientException, IOException {
                Map<String, String> params = new HashMap<>();
                params.put("key", "free");
                params.put("appid", "0");
                params.put("msg", "hello");
                return NetServer.<Response>getNetOperation().get(
                        "http://t.weather.sojson.com/api/weather/city/101030100",
                        null, null, null);
            }
        }.execute();
    }

    private void paizhao() {
        requestPermission(new PermissionCallBack.OnlyGranted() {
            @Override
            public void granted(Permission permission) {
                Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                Uri uri;
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(MyApp.getInstance(), "com.example.tys.baseapplication.fileprovider",
                            new File(Environment.getExternalStorageDirectory() + "/out.png"));
                } else {
                    uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/out.png"));
                }

                //为拍摄的图片指定一个存储的路径
                intent2.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(intent2, 10086);
            }
        }, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


    }


    @Override
    protected int setStatusBarColor() {
        return Color.RED;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected int setNavigationBarColor() {
        return Color.BLACK;

    }

}

